package com.acegear.horizon.domain.models.jpa;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import java.util.Optional;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Created by mercury on 2016/12/12.
 */
@Entity
public class WeiboAuth {

    @Column(unique = true)
    private Long userId;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @Column(unique = true, columnDefinition = "VARCHAR(100)")
    private String uid;

    public WeiboAuth() {
    }

    public WeiboAuth(Long userId, String uid) {
        this.userId = userId;
        this.uid = uid;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Optional<UserInfo> auth(String token) {
        String weiboUrl = "https://api.weibo.com/2/users/show.json";
        WeiboUserInfo weiboUserInfo = null;
        try {
            weiboUserInfo = Unirest.get(weiboUrl).queryString("uid", uid).queryString("access_token", token).asObject(WeiboUserInfo.class).getBody();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        if (weiboUserInfo == null || weiboUserInfo.isError()) {
            return Optional.empty();
        } else {
            UserInfo info = new UserInfo();
            info.setNickname(weiboUserInfo.getScreen_name());
            info.setAvatar(weiboUserInfo.getAvatar_hd());
            info.setGender(weiboUserInfo.getGender().equals("m") ? 0 : 1);
            info.setProvince(weiboUserInfo.getLocation());

            return Optional.of(info);
        }
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
class WeiboUserInfo {
    private String screen_name;
    private String avatar_hd;
    private String location;
    private String gender;
    private Integer province;
    private Integer city;
    private Integer error_code;
    private String error;

    public WeiboUserInfo() {

    }

    public String getScreen_name() {
        return screen_name;
    }

    public void setScreen_name(String screen_name) {
        this.screen_name = screen_name;
    }

    public String getAvatar_hd() {
        return avatar_hd;
    }

    public void setAvatar_hd(String avatar_hd) {
        this.avatar_hd = avatar_hd;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getProvince() {
        return province;
    }

    public void setProvince(Integer province) {
        this.province = province;
    }

    public Integer getCity() {
        return city;
    }

    public void setCity(Integer city) {
        this.city = city;
    }

    public Integer getError_code() {
        return error_code;
    }

    public void setError_code(Integer error_code) {
        this.error_code = error_code;
    }

    public String getError() {
        return error;
    }

    public boolean isError() {
        return error_code != null;
    }

    public void setError(String error) {
        this.error = error;
    }
}